package com.itheima.app.cron;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.User;
import com.itheima.service.db.UserService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RemainingTimes {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Reference
    private UserService userService;

//    @Scheduled(cron = "0 0 0 1/1 * ? ")  一天更新一次
    @Scheduled(cron = "0 0/10 * * * ? ")//9分钟更新一次
    public void updateTimes(){
        List<User> allUsers = userService.findAll();

        if(CollectionUtil.isNotEmpty(allUsers)){
            for (User user : allUsers) {
                stringRedisTemplate.opsForValue().set(ConstantUtil.VOICE_REMAININGTIMES+user.getId(),"4");
            }
        }
    }
}
